package com.yygo.webapp.spring;

import com.yygo.Constants;
import com.yygo.model.Feedback;
import com.yygo.model.LoanRequest;
import com.yygo.model.User;
import com.yygo.model.enums.FeedbackStatus;
import com.yygo.model.enums.FeedbackType;
import com.yygo.model.vo.FeedbackVO;
import com.yygo.service.FeedbackManager;
import com.yygo.service.LoanRequestManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.SecurityContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiansha on 2015/6/28.
 */
public class FeedbackInterceptorAdapter extends HandlerInterceptorAdapter {

    @Autowired
    private LoanRequestManager loanRequestManager;

    @Autowired
    private FeedbackManager feedbackManager;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        return true;
    }
    public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {
        if (response.isCommitted()) {
            return;
        }
        org.springframework.security.core.context.SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx.getAuthentication() != null) {
            Authentication auth = ctx.getAuthentication();
            if (auth.getPrincipal() instanceof User) {
                User currentUser = (User) auth.getPrincipal();
                List<Feedback> feedbackList = feedbackManager.getFeedbacks(currentUser);
                List<FeedbackVO> feedbackVOList = new ArrayList<>();
                List<LoanRequest> materialRequests = new ArrayList<>();
                for (Feedback feedback : feedbackList) {
                    FeedbackVO vo = new FeedbackVO(feedback);
                    if(feedback.getFeedbackStatus().equals(FeedbackStatus.Requested)&&feedback.getFeedbackTarget().equals(currentUser)){
                        feedbackVOList.add(vo);
                    }else if(feedback.getFeedbackStatus().equals(FeedbackStatus.Returned)&&feedback.getFeedbackRequester().equals(currentUser)){
                        feedbackVOList.add(vo);
                    }
                    if (feedback.getFeedbackType().equals(FeedbackType.MaterialType) && feedback.getFeedbackTarget().equals(currentUser)) {
                        materialRequests.add(feedback.getLoanRequest());
                    }
                }
                request.getSession().setAttribute("feedbacks", feedbackVOList);
                request.getSession().setAttribute("materialRequests", materialRequests);
                request.getSession().setAttribute("loggedUser", currentUser);
            }
        }
    }
    public void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}
